/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ftpcombiner.gui.applicationframe.framesplitter;

import ftpcombiner.ApplicationController;
import ftpcombiner.filesystem.FileSystem;
import ftpcombiner.gui.GUILoader;
import ftpcombiner.gui.applicationframe.framesplitter.filetable.FileContextMenu;
import ftpcombiner.gui.applicationframe.framesplitter.filetable.FileTableEditor;
import ftpcombiner.gui.applicationframe.framesplitter.filetable.FileTableModel;
import ftpcombiner.gui.applicationframe.framesplitter.filetable.FileTableRenderer;
import ftpcombiner.listeners.UploadDropTargetListener;
import ftpcombiner.server.virtualfiles.VirtualFolder;
import ftpcombiner.server.virtualfiles.VirtualItem;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.ListSelectionModel;
import javax.swing.event.ChangeEvent;

/**
 *
 * @author Ben
 */
public class FileTable extends JTable implements GUILoader {

    private FileTableModel listModel;
    //private FileListUI listUI;
    private FileContextMenu contextMenu;

    public FileTable() {
        init();
    }

    @Override
    public void initializeComponents() {
        listModel = new FileTableModel();
        contextMenu = new FileContextMenu(this);
    }

    @Override
    public void setLayouts() {
    }

    @Override
    public void addComponents() {
    }

    @Override
    public void addComponentListeners() {
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    if ((((FileTable) e.getSource()).getValueAt(getSelectedRow(), 0)) instanceof VirtualFolder) {
                        FileSystem.getInstance().changeDirectory(((FileTable) e.getSource()).getValueAt(getSelectedRow(), 0).toString());
                        //setModel(listModel);
                        //((FileListUI)getUI()).updateContents();
                    } else if (((FileTable) e.getSource()).getValueAt(getSelectedRow(), 0).equals("Up...")) {
                        FileSystem.getInstance().changeDirectoryUp();
                    }
                } else if (e.getClickCount() == 1 && getSelectedRowCount() == 1) {
                    if (!FileTableEditor.renameableSelection) {
                        FileTableEditor.time = System.currentTimeMillis();
                    }
                    FileTableEditor.renameableSelection = true;

                } else {
                    FileTableEditor.renameableSelection = false;
                }
            }
        });
    }

    @Override
    public void editingStopped(ChangeEvent e) {
        if (!((VirtualItem) getValueAt(getEditingRow(), 0)).getName().equals(((JTextField) editorComp).getText())) {
            //System.out.println(getRow(getEditingRow()).toString());
            FileSystem.getInstance().rename((VirtualItem) getValueAt(getEditingRow(),0), ((JTextField) editorComp).getText());
        }
        removeEditor();

    }

    public Object getRow(int i) {
        return getValueAt(i, 0);
    }

    @Override
    public void otherInit() {
        setModel(listModel);
        setSelectionMode(ListSelectionModel.MULTIPLE_INTERVAL_SELECTION);
        //setSelectionModel(new FileTableSelectionModel(this));
        setDropTarget(new UploadDropTargetListener());
        setCellEditor(new FileTableEditor());
        getColumn(getColumnName(0)).setCellRenderer(new FileTableRenderer());
        setShowGrid(false);
        getColumn(getColumnName(0)).setCellEditor(new FileTableEditor());
        ApplicationController.getSplashScreen().incrementProgress();
    }

    private void init() {
        initializeComponents();
        setLayouts();
        addComponents();
        addComponentListeners();
        otherInit();
    }
}
